package com.server;

import java.util.ArrayList;
import java.util.List;

import com.dao.ILocusDao;

public class ILocusServiceImpl implements ILocusService {
	public ILocusDao iLocusDao;

	/**
	 * 查找轨迹
	 * 
	 * @param idsArry
	 * @return
	 */
	public String searchLocus(String idsArry) {
		String[] idArry = idsArry.split(",");
		List l = iLocusDao.findsubOrder(idArry);// 查找子单号
		String trackId = "";
		String trackSubRecode = "";
		String jsonStr = "[";
		if(l!=null){
		for (int i = 0; i < l.size(); i++) {
			trackSubRecode = "";
			trackId = "";
			trackId = ((Object[]) l.get(i))[0] + "";
			List locusList = iLocusDao.findLocus(trackId);
			for (int j = 0; j < locusList.size(); j++) {
				if (j == 0) {
					trackSubRecode += "{'scanTime':'"+((Object[])locusList.get(j))[0]+"','trackLocus':'"+trackData(((Object[])locusList.get(j))[3]+"",((Object[])locusList.get(i))[4]+"",((Object[])locusList.get(j))[5]+"")+"'}";
				} else {
					trackSubRecode += ",{'scanTime':'"+((Object[])locusList.get(j))[0]+"','trackLocus':'"+trackData(((Object[])locusList.get(j))[3]+"",((Object[])locusList.get(i))[4]+"",((Object[])locusList.get(j))[5]+"")+"'}";
				}
			}
			if(i==0){
				jsonStr += "{" + "'trackId':'" + trackId + "'," + "'trackRecode':["+ trackSubRecode + "]" + "}";
			}else{
				jsonStr += ",{" + "'trackId':'" + trackId + "'," + "'trackRecode':["+ trackSubRecode + "]" + "}";
			}
		}
		}else{
		}
		/*List<String> list = new ArrayList<String>();
		list.add("1");
		list.add("2");
		list.add("3");*/
		return jsonStr + "]";
	}

	/**
	 * 组织轨迹语言
	 * 
	 * @param data
	 * @return
	 */
	private String trackData(String action, String network_name,
			String dstareacode) {
		System.out.println(action);
		String aa = "";
		if ("收件".equals(action.trim())) {
			aa = "收件员已收取快件";
		} else if ("入库".equals(action.trim())) {
			aa = "快件已到达 [" + network_name + "]";
		} else if ("分公司发件".equals(action.trim())) {
			aa = "快件从 [" + network_name + "] 发往 [" + dstareacode + "]";
		} else if ("中转到件".equals(action.trim())) {
			aa = "快件已到达 [" + network_name + "]";
		} else if ("中转发件".equals(action.trim())) {
			aa = "快件从 [" + network_name + "] 发往 [" + dstareacode + "]";
		} else if ("分公司到件".equals(action.trim())) {
			aa = "快件已到达 [" + network_name + "]";
		} else if ("签收".equals(action.trim())) {
			aa = "快件已签收,签收人是 [" + dstareacode + "]";
		} else if ("退件".equals(action.trim())) {
			aa = "货物退件";
		}
		return aa;
	}

	public ILocusDao getiLocusDao() {
		return iLocusDao;
	}

	public void setiLocusDao(ILocusDao iLocusDao) {
		this.iLocusDao = iLocusDao;
	}
}
